package com.example.springboot_c.Controller;

import com.example.springboot_c.Service.UserAccountService;
import com.example.springboot_c.pojo.UserAccount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/user")
public class UserAccountController {
    @Autowired
    private UserAccountService userService;

    @PostMapping("/login")
    public String login(@RequestParam String username, @RequestParam String password, Model model) {
        UserAccount user = userService.login(username, password);
        if (user != null) {
            model.addAttribute("user", user);
            return "dashboard";
        }
        model.addAttribute("error", "用户名或密码错误");
        return "login";
    }

    @PostMapping("/register")
    public String register(UserAccount user) {
        userService.register(user);
        return "redirect:/user/login";
    }

    @GetMapping("/list")
    public String list(Model model) {
        model.addAttribute("users", userService.findAll());
        return "admin/user_list";
    }
}